<template>
  <!-- 银行信息 -->
  <div>
    <curd-title v-if="infoFlg" :title="'银行信息'" />
    <avue-crud
      ref="banks"
      :table-loading="bankLoading"
      :option="bankOption"
      :data="bankList"
      v-model="bankForm"
      @on-load="onBankLoad"
      @row-save="rowBankSave"
      @row-update="rowBankUpdate"
      @refresh-change="refreshBankChange"
      @row-del="rowBankDel"
      :before-open="beforeBankOpen"
      :row-class-name="delRowClassName"
    >
      <template slot="bankNameForm">
        <el-select
          v-model="bankForm.bankName"
          filterable
          remote
          reserve-keyword
          placeholder="请输入关键词"
          :remote-method="remoteBankNameMethod"
          :loading="bankNameLoading"
        >
          <el-option v-for="item in bankOptions" :key="item.id" :label="item.bankName" :value="item.bankName">
          </el-option>
        </el-select>
      </template>
      <template slot="bankCodeForm">
        <el-select
          v-model="bankForm.bankCode"
          filterable
          remote
          reserve-keyword
          placeholder="请输入关键词"
          :remote-method="remoteBankCodeMethod"
          :loading="bankCodeLoading"
        >
          <el-option v-for="item in bankCodeOptions" :key="item.id" :label="item.bankCode" :value="item.bankCode">
          </el-option>
        </el-select>
      </template>
      <template slot="bankAccountName" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.bankAccountNameFlag === '1'">{{ scope.row.bankAccountName }}</del>
          {{ scope.row.bankAccountNameA }}
        </div>
        <span v-else>{{ scope.row.bankAccountName }}</span>
      </template>
      <template slot="bankAccount" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.bankAccountFlag === '1'">{{ scope.row.bankAccount }}</del>
          {{ scope.row.bankAccountA }}
        </div>
        <span v-else>{{ scope.row.bankAccount }}</span>
      </template>
      <template slot="bankName" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.bankNameFlag === '1'">{{ scope.row.bankName }}</del>
          {{ scope.row.bankNameA }}
        </div>
        <span v-else>{{ scope.row.bankName }}</span>
      </template>
      <template slot="bankCode" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.bankCodeFlag === '1'">{{ scope.row.bankCode }}</del>
          {{ scope.row.bankCodeA }}
        </div>
        <span v-else>{{ scope.row.bankCode }}</span>
      </template>
    </avue-crud>
  </div>
</template>

<script>
import { bankOption } from '@/option/tenant/supplier/finance-info'
import CurdTitle from '@/components/base/title'
import { getBankList, getBankInfo, removeBank, addBank, updateBank } from '@/api/supplier/bank'
import { getStore } from '@/util/store'
import delRowClassName from '@/views/plugin/workflow/mixins/info'

export default {
  name: 'BankInfo',
  components: { CurdTitle },
  inject: {
    type: {
      default: 'view'
    },
    infoFlg: {
      default: true
    },
    slotFlg: {
      default: false
    }
  },
  mixins: [delRowClassName],
  data() {
    return {
      supplierId: getStore({ name: 'supplierId' }),
      bankLoading: false,
      bankList: [],
      bankOption: bankOption.call(this),
      bankForm: {},
      bankOptions: [],
      bankCodeOptions: [],
      banks: [],
      bankNameLoading: false,
      bankCodeLoading: false
    }
  },
  watch: {
    'bankForm.bankName'(val) {
      this.bankForm.bankCode = this.banks.filter(i => {
        return i.bankName === val ? i.bankCode : ''
      })[0].bankCode
    },
    'bankForm.bankCode'(val) {
      this.bankForm.bankName = this.banks.filter(i => {
        return i.bankCode === val ? i.bankName : ''
      })[0].bankName
    }
  },
  methods: {
    beforeBankOpen(done, type) {
      if (['edit', 'add'].includes(type)) {
        this.getBanks()
      }
      done()
    },
    getBanks() {
      getBankInfo().then(res => {
        this.banks = res.data.data
      })
    },
    remoteBankNameMethod(query) {
      if (query !== '') {
        this.bankNameLoading = true
        setTimeout(() => {
          this.bankNameLoading = false
          this.bankOptions = this.banks.filter(item => {
            return item.bankName.toLowerCase().indexOf(query.toLowerCase()) > -1
          })
        }, 200)
      } else {
        this.bankOptions = []
      }
    },
    remoteBankCodeMethod(query) {
      if (query !== '') {
        this.bankCodeLoading = true
        setTimeout(() => {
          this.bankCodeLoading = false
          this.bankCodeOptions = this.banks.filter(item => {
            return item.bankCode.toLowerCase().indexOf(query.toLowerCase()) > -1
          })
        }, 200)
      } else {
        this.bankCodeOptions = []
      }
    },
    onBankLoad() {
      this.infoFlg
        ? ((this.bankLoading = true),
          getBankList({ supplierId: this.supplierId }).then(res => {
            this.bankList = res.data.data
            this.bankLoading = false
          }))
        : ''
    },
    refreshBankChange() {
      this.onBankLoad()
    },
    rowBankSave(row, done, loading) {
      addBank({ ...row, supplierId: this.supplierId }).then(
        () => {
          this.onBankLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        error => {
          window.console.log(error)
          loading()
        }
      )
    },
    rowBankUpdate(row, index, done, loading) {
      updateBank(row).then(
        () => {
          this.onBankLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        error => {
          window.console.log(error)
          loading()
        }
      )
    },
    rowBankDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return removeBank(row.id)
        })
        .then(() => {
          this.onBankLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
    }
  }
}
</script>

<style></style>
